package com.liulimi.fastapi.application.controller;

import com.liulimi.fastapi.application.bean.UserBean;
import com.liulimi.fastapi.core.annotation.DateValidator;
import com.liulimi.fastapi.core.annotation.IsMobile;
import com.liulimi.fastapi.core.annotation.Signature;
import com.liulimi.fastapi.core.exception.asserts.Assert;
import com.liulimi.fastapi.core.exception.asserts.AuthSignatureAssert;
import com.liulimi.fastapi.core.http.Request;
import com.liulimi.fastapi.core.http.response.Response;
import com.liulimi.fastapi.core.http.response.Result;
import com.liulimi.fastapi.core.http.response.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Api("测试demo")
@RestController
public class Demo {
    private static final Logger log = LoggerFactory.getLogger(Demo.class);

    @ApiOperation("测试成功")
    @RequestMapping(value = "success",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"}
            )
    @ResponseBody()
//    @PreAuthorize("hasAuthority('p_transfer')")
    public Result<UserBean> helloSuccess(){
        UserBean userBean = new UserBean("陈豪","男",11);
        log.info("I am info log!");
        return Response.success(userBean);
    }


    @ApiOperation("hello World")
    @RequestMapping(value = "hello",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"}
    )
    @ResponseBody()
//    @PreAuthorize("isAnonymous()")
    public Result<String> hello(){
//        UserBean userBean = new UserBean("陈豪","男",11);
//        log.info("I am info log!");
        return Response.success("hello world");
    }


    @GetMapping("fail")
    public Result<String> helloFail(){
        return Response.fail("我是操作失败消息");
    }

    @PostMapping(value = "test")
    @Signature
    public String myController(@RequestBody @Valid Request<User> user) {
        return String.join(",", user.toString());
    }

    @PostMapping(value = "exception")
    public String exceptionTest() {
        return AuthSignatureAssert.getInstance().doAssert("",ResultEnum.ERROR,"test");
    }

    private static class User {

        @IsMobile(message = "手机号格式不正确")
        private String mobile;
        @NotNull
        private int age;
        @DateValidator
        private String date;

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public String getMobile() {
            return mobile;
        }

        public void setMobile(String mobile) {
            this.mobile = mobile;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return new ToStringBuilder(this)
                    .append("mobile", mobile)
                    .append("age", age)
                    .toString();
        }
    }

}
